﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class ABINFONData : BaseFunction
    {
        public int ThreadCount;
        private string OrderNum;
       
        public ABINFONData()
            : base()
        {

        }
        public ABINFONData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

       public ABINFONData(OracleConnection _OracleDB, IntexReptServBLClient _proxy)
            : base(_OracleDB, _proxy)
        {

        }
                public ABINFONData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, INTEXDataSet _setUI)
            : base(_OracleDB, _proxy)
        {
            this.setUI = _setUI;
        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult= new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand(); 
                  or1.CommandText=  "SELECT distinct '' AS FORDERNUM,'' AS VKPAUFNUM,'' AS ARTICLE,VKPAUFKRS,VKPAUFLFD,"
                    + " VKPAUFPOS,VKPNEUDAT,VKPAENDDAT,KDKUNDNA"
                    + ",VKPWUTERM,'5' AS REMAINDAYS, VKPMEEH,VKPMENGE1, VKPLIEFTERM, VKPLIEFTERM AS SALESREPLAY,Round(ABASANZSTK*ABASSTKLG*0.001/0.9144,1) AS ABARESQTY,'0' AS FNVKDIFF,VKPRESERV,VKPZUGET"
                    + ",VKPKDARTNR||VKPKDBEZ||VKPKDFARBNR AS STDBATCHNO,'NO' as Encoded,SUBSTR(SDDATEN,2,10) AS VKPPOSTYP"
                    + ",P.ABPAUFKRS, P.ABPAUFLFD,P.ABPAUFPOS,P.ABPART,ABPERLDAT AS FNAENDDATE,"
                    + "P.ABPVAR,P.ABPFARBE,P.ABPBESCHICH,P.ABPRESMGE,P.ABPSOLLMENGE1 AS FPLANLENG, ABPRESSTK, ABPEALFM AS FACTLENG"
                    + ", P.ABPZUGSTK,ABPLIEFDAT AS FNPENDDATE,'0' AS GMETER,'0' AS FMETER"
                    + "  FROM VKPOS K LEFT JOIN SDDAT ON VKPMANDANT = SUBSTR(SDKEY,1,2)  AND VKPPOSTYP = SUBSTR(SDKEY,3,3) AND SDSA = '0253' AND SUBSTR(SDKEY,24,1) = 'V'"
                    + "  LEFT JOIN ABAUFM A ON A.ABAVKAUFKRS=K.VKPAUFKRS AND A.ABAVKAUFLFD=K.VKPAUFLFD  AND A.ABAVKAUFPOS=K.VKPAUFPOS"
                    + "  LEFT JOIN ABPOS  P ON A.ABAAUFKRS=P.ABPAUFKRS AND A.ABAAUFLFD=P.ABPAUFLFD  AND A.ABAAUFPOS=P.ABPAUFPOS"
                    + "  LEFT JOIN KDSTAMM ON KDKUNDNR=VKPKUNDNR"
                    + "  WHERE VKPSA<>'AJ' AND VKPERL<>'J' "
                    + " AND VKPAUFKRS='11'  AND ABPERLKZ=' '"
                   //+" AND VKPAUFKRS||VKPAUFLFD||VKPAUFPOS='11000047001'"
                    + " ORDER BY VKPAUFKRS,VKPAUFLFD,VKPAUFPOS";
                  setLocal = Connect2Proxy(or1);
                
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    SetDefault(tableLocal);
                    Queue Q100 = new Queue();
                    Q100.Enqueue("FACTLENG");
                    Q100.Enqueue("FPLANLENG");
                    Q100.Enqueue("VKPMENGE1");
                    Q100.Enqueue("VKPRESERV");
                    Q100.Enqueue("VKPZUGET");
                    DivideBy100(tableLocal, Q100); 

                    byte[] b;
                    string value;
                    string GISSUEDATE = "", sGISSUEDATE;
                    decimal GMETER = 0, sGMETER;
                    decimal GPCNUM = 0, sGPCNUM;
                    string sTemp = "";
                    string SIZEBATCHNO = "";
                    string VKPWUTERM = "";
                    int Year, Month, Day;
                    string Column = "";
                    Double VKPMENGE1 = 0, FACTLENG = 0, FinishedLength = 0;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "KDKUNDNA";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "ABPFARBE";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "VKPPOSTYP";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "STDBATCHNO";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["ABPBESCHICH"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["ABPBESCHICH"] = value;
                        VKPWUTERM = dwTemp["VKPWUTERM"].ToString().Trim();
                        if (VKPWUTERM != "0")
                        {
                            Year = System.Convert.ToInt16(VKPWUTERM.Substring(0, 4));
                            Month = System.Convert.ToInt16(VKPWUTERM.Substring(4, 2));
                            Day = System.Convert.ToInt16(VKPWUTERM.Substring(6, 2));
                            DateTime del = new DateTime(Year, Month, Day);
                            dwTemp["REMAINDAYS"] = Math.Floor(del.Subtract(DateTime.Now).TotalDays).ToString();
                        }
                        VKPMENGE1 = System.Convert.ToDouble(dwTemp["VKPMENGE1"].ToString());
                        FACTLENG = System.Convert.ToDouble(dwTemp["FACTLENG"].ToString());
                        if (FACTLENG > 0)
                        {
                            dwTemp["FNVKDIFF"] = FACTLENG / 0.9144 - VKPMENGE1;
                        }
                        dwTemp["FORDERNUM"] = dwTemp["ABPAUFKRS"].ToString()
                            + "-" + dwTemp["ABPAUFLFD"].ToString() + "-" + dwTemp["ABPAUFPOS"].ToString();
                        dwTemp["VKPAUFNUM"] = dwTemp["VKPAUFKRS"].ToString()
                            + "-" + dwTemp["VKPAUFLFD"].ToString() + "-" + dwTemp["VKPAUFPOS"].ToString();
                        dwTemp["ARTICLE"] = dwTemp["ABPART"].ToString().Trim()
                            + "-" + dwTemp["ABPFARBE"].ToString().Trim() + "-" + dwTemp["ABPVAR"].ToString().Trim();

                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public void ExploreByWeavingOrder(DataRow dwUIABPOS, DataRow dwABPOS, string OrderNum, ref ArrayList listDIOrder, ref ArrayList listPROrder)
        {
            try
            {
                BaseFunction m_Data;
                m_Data = new AB55AV95DataIN();
                m_Data.proxy = this.proxy;
                m_Data.OracleDB = this.OracleDB;
                dwUIABPOS["ABPAUFKRS"] = OrderNum.Substring(0, 2);
                dwUIABPOS["ABPAUFLFD"] = OrderNum.Substring(3, 6);
                dwUIABPOS["ABPAUFPOS"] = OrderNum.Substring(10, 3);
                dwUIABPOS["WBAUFKRS"] = OrderNum.Substring(0, 2);
                dwUIABPOS["WBAUFLFD"] = OrderNum.Substring(3, 6);
                dwUIABPOS["WBAUFPOS"] = OrderNum.Substring(10, 3);
                dwUIABPOS["ABPART"] = "";
                setResult = ((AB55AV95DataIN)m_Data).GetABPOS(dwUIABPOS);
                double GreigePlanLength = 0, WFAENDDATE = 0, ZUNEUDAT = 0;
                WFAENDDATE = System.Convert.ToDouble(dwABPOS["WFAENDDATE"].ToString());
                string sTemp = "", OrderType="";
                foreach (DataRow dwTemp in setResult.Tables["ABPOS"].Rows)
                {
                    OrderType = dwTemp["ABPAUFART"].ToString().Trim();
                    if (OrderType == "DI")
                    {
                        dwABPOS["GREIGEPLANLENGTH"] = dwTemp["ABPSOLLMENGE1"];
                    }
                    DataSet setTemp6 = ((AB55AV95DataIN)m_Data).GetMATYARNINFO(dwUIABPOS,dwABPOS);
                    foreach (DataRow dwTemp6 in setTemp6.Tables["GAAUF"].Rows)
                    {
                        ZUNEUDAT = System.Convert.ToDouble(dwTemp6["ZUNEUDAT"].ToString());
                        if (WFAENDDATE < ZUNEUDAT)
                        {
                            WFAENDDATE = ZUNEUDAT;
                        }
                    }
                }
                dwABPOS["WFAENDDATE"] = WFAENDDATE;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void BuildABPOS(DataRow dwABPOS)
        {
            #region ------------------Property--------------------------
            base.BuildABPOS(dwABPOS);
            BaseFunction m_Data;
            DataRow dwUIABPOS = dwABPOS.Table.Clone().NewRow();
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
            dwNABPOS.ItemArray = dwABPOS.ItemArray;
            DataSet set4Return = new DataSet();
            ABPOS = dwNABPOS["ABPAUFKRS"].ToString() + "." + dwNABPOS["ABPAUFLFD"].ToString() + "." + dwNABPOS["ABPAUFPOS"].ToString();
            this.Name = ABPOS;
            double GRALENG = 0, GreigePlanLength = 0;
            Tracer.Verbose(ThreadID.ToString(), "查询开始:[" + ThreadID.ToString() + "] :" + ABPOS);
            #endregion
            try
            {
                #region-----------------------从后整到织造----------------------------
                AB55AV95DataIN m_Data55 = new AB55AV95DataIN();
                m_Data55.proxy = this.proxy;
                m_Data55.OracleDB = this.OracleDB;
                AB05AV95DataIN m_Data05 = new AB05AV95DataIN();
                m_Data05.proxy = this.proxy;
                m_Data05.OracleDB = this.OracleDB;
                ArrayList listDIOrder = new ArrayList(), listPROrder = new ArrayList();
                DataSet setTempA = m_Data05.GetReservedOrder(dwNABPOS, ref listDIOrder, ref listPROrder);
                foreach (string orderPR in listPROrder)
                {
                    ExploreByWeavingOrder(dwUIABPOS, dwNABPOS, orderPR, ref listDIOrder, ref listPROrder);
                }
                DataSet setTemp1 = new DataSet();
                DataSet set5AB55 = new DataSet();
                foreach (string orderDI in listDIOrder)
                {
                    ExploreByWeavingOrder(dwUIABPOS, dwNABPOS, orderDI, ref listDIOrder, ref listPROrder);
                    setTemp1 = m_Data55.GetReservedOrder(dwNABPOS, ref listDIOrder, ref listPROrder, orderDI);
                    set5AB55.Merge(setTemp1);
                }
                #endregion

                #region-----------------------从织造到浆纱---------------------------
                double BeamLength = 0;
                double BEAMALENG = 0, FPLANLENG = 0;
                int SBEAMNO = 0, ABEAMNO = 0;
                string OrderType = "";
                m_Data = new AB45AV95DataIN();
                m_Data.proxy = this.proxy;
                m_Data.OracleDB = this.OracleDB;
                if (set5AB55.Tables.Contains("RESERVEDORDER"))
                {
                    foreach (DataRow dwTemp in set5AB55.Tables["RESERVEDORDER"].Rows)
                    {
                        OrderType = dwTemp["ABPAUFART"].ToString().Trim();
                        if (OrderType != "VW")
                            continue;
                        OrderNum = dwTemp["ABPAUFKRS"].ToString() + "-" + dwTemp["ABPAUFLFD"].ToString() + "-" + dwTemp["ABPAUFPOS"].ToString();
                        ((AB45AV95DataIN)m_Data).GetReservedOrder(dwNABPOS, ref listDIOrder, ref listPROrder, OrderNum);
                    }
                }
                #endregion
                #region-----------------------收货信息----------------------------


                FPLANLENG = System.Convert.ToDouble(dwNABPOS["FPLANLENG"].ToString());
                BEAMALENG = System.Convert.ToDouble(dwNABPOS["BEAMALENG"].ToString());
                if (FPLANLENG > 0)
                {
                    dwNABPOS["PLANFNVWDIFF"] = Math.Round(BEAMALENG / FPLANLENG, 3);
                }
                BeamLength = System.Convert.ToDouble(dwNABPOS["SBEAMLENG"].ToString());
                GRALENG = System.Convert.ToDouble(dwNABPOS["GRALENG"].ToString());
                GreigePlanLength = System.Convert.ToDouble(dwNABPOS["GREIGEPLANLENGTH"].ToString());
                dwNABPOS["VWDIFFAP"] = BEAMALENG - BeamLength;
                if (GRALENG > 0)
                {
                    dwNABPOS["ACTWEAVEIN"] = Math.Round(BEAMALENG / GRALENG, 2).ToString();
                }
                if (BeamLength > 0)
                    dwNABPOS["GRVWDIFF"] = Math.Round((BeamLength - GreigePlanLength) * 100 / BeamLength, 2);

                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                try
                {
                    decimal GMETER = System.Convert.ToDecimal(dwNABPOS["GMETER"].ToString());
                    decimal FMETER = System.Convert.ToDecimal(dwNABPOS["FMETER"].ToString());
                    decimal FGWASTAGE = 0;
                    if (FMETER > 0)
                    {
                        FGWASTAGE = Math.Round(100 * GMETER / FMETER, 2);
                    }
                    dwNABPOS["FGWASTAGE"] = FGWASTAGE;
                    dwABPOS.ItemArray = dwNABPOS.ItemArray;

                    string progress = "查询进展:[" + ThreadID.ToString() + "] 完成<-FinishedJobs->/<-TotalJobs->";
                    EventDispatcher.Instance.RaiseStatus(progress, StatusEventType.StartProgress);

                }
                catch (Exception ex)
                {
                    Tracer.Error(ABPOS + ex.StackTrace, ex);
                }
            }
        }
    }
}